package com.yqb.study.modular.dao.redis;

import java.util.concurrent.TimeUnit;

import org.springframework.data.redis.core.StringRedisTemplate;

import com.alibaba.fastjson.JSON;
import com.yqb.study.constant.key.redis.IKeyPrefix;
import com.yqb.study.util.ReflectionUtils;

public class RedisBaseDao<T> {
	// 操作redis的连接
	private StringRedisTemplate stringRedisTemplate = null;

	public RedisBaseDao(StringRedisTemplate stringRedisTemplate) {
		this.stringRedisTemplate = stringRedisTemplate;
	}
	
	/**
     * 获取需要操作的实体类class
     * 例如: CityRedisDao extends RedisBaseDao<City>，返回的是City的Class
	 */
    private Class<T> getEntityClass() {
        return ReflectionUtils.getSuperClassGenricType(getClass());
    }	

	/**
	 * 将bean转成json存入redis
	 */
	private String beanToString(T value) {
		if (value == null) {
			return null;
		}
		
		Class<?> clazz = value.getClass();
		if (clazz == int.class || clazz == Integer.class) {
			return "" + value;
		} else if (clazz == String.class) {
			return (String) value;
		} else if (clazz == long.class || clazz == Long.class) {
			return "" + value;
		} else {
			return JSON.toJSONString(value);
		}
	}

	/**
	 * 从redis中取出json转成对应的bean对象
	 */
	private T stringToBean(String str, Class<T> clazz) {
		if (str == null || str.length() <= 0 || clazz == null) {
			return null;
		}
		if (clazz == int.class || clazz == Integer.class) {
			return (T) Integer.valueOf(str);
		} else if (clazz == String.class) {
			return (T) str;
		} else if (clazz == long.class || clazz == Long.class) {
			return (T) Long.valueOf(str);
		} else {
			return JSON.toJavaObject(JSON.parseObject(str), clazz);
		}
	}

	/**
	 * IKeyPrefix prefix为不同的变量加上指定前缀
	 * 获取key对应的单个bean对象
	 */
	public T get(IKeyPrefix prefix, String key) {
		// 生成真正的key
		String realKey = prefix.getPrefix() + key;

		String beanStr = stringRedisTemplate.opsForValue().get(realKey);
		return stringToBean(beanStr, getEntityClass());
	}
	
	/**
	 * IKeyPrefix prefix为不同的变量加上指定前缀
	 * 设置对象
	 * */
	public boolean set(IKeyPrefix prefix, String key, T value) {
		String beanStr = beanToString(value);
		if (beanStr == null || beanStr.length() <= 0) {
			return false;
		}
		
		// 生成真正的key
		String realKey = prefix.getPrefix() + key;
		int seconds = prefix.expireSeconds();
		if (seconds <= 0) {
			stringRedisTemplate.opsForValue().set(realKey, beanStr);
		} else {
			stringRedisTemplate.opsForValue().set(realKey, beanStr, seconds, TimeUnit.SECONDS);
		}
		return true;
	}

	/**
	 * IKeyPrefix prefix为不同的变量加上指定前缀
	 * 判断key是否存在
	 */
	public boolean exists(IKeyPrefix prefix, String key) {
		// 生成真正的key
		String realKey = prefix.getPrefix() + key;

		return stringRedisTemplate.hasKey(realKey);
	}

	/**
	 * IKeyPrefix prefix为不同的变量加上指定前缀
	 * 删除key对应键值对
	 */
	public void delete(IKeyPrefix prefix, String key) {
		// 生成真正的key
		String realKey = prefix.getPrefix() + key;
		
		if (stringRedisTemplate.hasKey(realKey)) {
			stringRedisTemplate.delete(realKey);
		}
	}	

//	/**
//	 * 增加值
//	 */
//	public Long incr(IKeyPrefix prefix, String key) {
//		Jedis jedis = null;
//		try {
//			jedis = jedisPool.getResource();
//			// 生成真正的key
//			String realKey = prefix.getPrefix() + key;
//			return jedis.incr(realKey);
//		} finally {
//			returnToPool(jedis);
//		}
//	}
//
//	/**
//	 * 减少值
//	 */
//	public Long decr(IKeyPrefix prefix, String key) {
//		// 生成真正的key
//		String realKey = prefix.getPrefix() + key;
//		return jedis.decr(realKey);
//	}
}
